﻿using System;
using System.Web.UI;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Data;
using System.Text;
using MaLu.Data;
using MaLu.Framework.Common;

namespace MaluJg.web.Three
{
    public partial class Three2Detail : ThreePage
    {
        protected string CanAdd = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.UseTabber = true;
            Title = "创建事项";
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            int status = (int)MaLu.Common.Const.EventStatus.已提交;
            btnFinish.Visible = false;
            if (EventID != Guid.Empty)
            {

                DataSet ds = BLThree.GetEvent2(Provider, EventID);
                if (ds.Tables.Count >= 2)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Title = "编辑事项";
                        DataRow drInfo = ds.Tables[0].Rows[0];
                        status = int.Parse(drInfo["Status"].ToString());
                        tbDept.Text = drInfo["DeptDspName"].ToString();
                        tbSign.Text = drInfo["Signature"].ToString();
                        tbYear.Text = drInfo["year"].ToString();
                        tbMonth.Text = drInfo["month"].ToString();
                        tbDay.Text = drInfo["day"].ToString();
                        if (IsLeader)
                            Title = "审阅事项";


                    }

                    DataTable dtIns = ds.Tables[1];
                    string temp = string.Empty;
                    foreach (DataRow item in dtIns.Rows)
                    {

                        temp += ";" + item["Name"].ToString().Replace( ";","@1").Replace( "|","@2").Replace("''", "'") + "|" + item["IsMost"].ToString() + "|" + item["IsHRChange"].ToString() + "|" +
                             item["IsArrage"].ToString() + "|" + item["IsMoneyUse"].ToString() + "|" + item["MakeTime"].ToString().Replace(";", "@1").Replace("|", "@2").Replace("''", "'") + "|" +
                                  item["IsAllPass"].ToString() + "|" + item["IsMinorOBeyMajor"].ToString() + "|" + item["IsDone"].ToString()
                                  + "|" + item["IsIng"].ToString() + "|" + item["IsUnDo"].ToString() + "|" + item["Change"].ToString().Replace(";", "@1").Replace("|", "@2").Replace("''", "'") + "|"
                                  + item["Remark"].ToString().Replace(";", "@1").Replace("|", "@2").Replace("''", "'");


                    }
                    tbInstances.Text = temp.Trim(new char[] { ';' });
                }


               
            }
            if (EventID != Guid.Empty)
            {
                if (IsLeader)
                {
                    tbDept.ReadOnly = true;
                    tbYear.ReadOnly = true;
                    tbMonth.ReadOnly = true;
                    tbDay.ReadOnly = true;



                    btnStart.Text = "<span><img src=\"../Images/Icon/Object/File-add.gif\" /></span><span>审阅</span>";

                }
                else
                {
                    tbSign.ReadOnly = true;

                }
            }
            else
            {
                tbSign.ReadOnly = true;

            }
            if (status == (int)MaLu.Common.Const.EventStatus.已审阅)
            {
                CanAdd = "1";
                btnStart.Visible = false;

            }
            else
                btnFinish.Visible = true;
        }
        protected Guid EventID
        {
            get
            {
                Guid gd = Guid.Empty;
                try
                {
                    gd = new Guid(Request["EventID"] ?? Guid.Empty.ToString());

                }
                catch { }

                return gd;

            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int result = 0;
            int status = (int)MaLu.Common.Const.EventStatus.已提交;
            string alert = "已提交！";
            if (IsLeader)
            {

                status = (int)MaLu.Common.Const.EventStatus.已审阅;
                alert = "已审阅！";
            }

            Guid gd = EventID == Guid.Empty ? Guid.NewGuid() : EventID;
            result = BLThree.SaveEvent2(Provider, gd, CurrentUser.MainDepartmentID, tbDept.Text, status,
               tbSign.Text, tbYear.Text, tbMonth.Text, tbDay.Text, CurrentUser.Name, DateTime.Now, CurrentUser.Name);

            if (result == 1 && !IsLeader)
            {
                alert = "已审阅,不能再提交！";

            }
            else
            {

                StringBuilder sb = new StringBuilder();
                string[] trs = tbInstances.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                sb.AppendLine("delete from TEvent2Instance where EventID='" + gd + "'");
                foreach (string item in trs)
                {
                    string[] tds = item.Split(new char[] { '|' });
                    sb.AppendFormat(@"INSERT INTO [TEvent2Instance]
           ([EventID] ,[Name],[IsMost] ,[IsHRChange],[IsArrage] ,[IsMoneyUse],[MakeTime],[IsAllPass]
           ,[IsMinorOBeyMajor],[IsDone],[IsIng],[IsUnDo] ,[Change] ,[Remark])  VALUES
           ('{0}',{1},{2},{3},{4},{5},'{6}',{7},{8},{9},{10},{11},'{12}','{13}');", gd, Util.ReturnSqlValue(tds[0].Replace("@1", ";").Replace("@2", "|"))
                                                                                    , bool.Parse(tds[1]) ? 1 : 0, bool.Parse(tds[2]) ? 1 : 0, bool.Parse(tds[3]) ? 1 : 0
                                                                                   , bool.Parse(tds[4]) ? 1 : 0, tds[5].Replace("@1", ";").Replace("@2", "|").Replace("'","''")
                                                                                   , bool.Parse(tds[6]) ? 1 : 0, bool.Parse(tds[7]) ? 1 : 0, bool.Parse(tds[8]) ? 1 : 0, bool.Parse(tds[9]) ? 1 : 0
                                                                                   , bool.Parse(tds[10]) ? 1 : 0, tds[11].Replace("@1", ";").Replace("@2", "|").Replace("'", "''"), tds[12].Replace("@1", ";").Replace("@2", "|").Replace("'", "''"));
                }
                SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());
            
            }

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('" + alert + "');location=location;</script>");

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int result = 0;

            string alert = "已删除！";


              result = BLThree.DeleteEvent2(Provider, EventID == Guid.Empty ? Guid.NewGuid() : EventID);

            if (result == 1)
            {
                alert = "已审阅,不能删除！";

            }
            if (result == 0)

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('" + alert + "');window.parent.location.href=window.parent.location.href;</script>");
            if (result == 1)

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('" + alert + "');location=location;</script>");

        }
    }
}
